﻿/// <reference path="jquery-1.8.0.js" />
/// <reference path="knockout-2.1.0.js" />
/// <reference path="knockout.mapping-latest.js" />
/// <reference path="central.core.js" />
/// <reference path="central.controls.js" />

var demo = demo || {};

(function (demo) {
    "use strict";
    demo.vm = demo.vm || {};
    demo.services = demo.services || {};
    var queryParams =
       {
           "EntityName": "Person",
           "Filters": {
               "LogicalOperator": "Or",
               "FilterParams": [
                   { "Name": "Age", "Operator": "IsGreaterThan", "Value": 13, "DisplayName": "Возраст" },
                   { "Name": "Age", "Operator": "IsLessThan", "Value": 31, "DisplayName": "Возраст" }
               ],
               "JsonFilterManagers": [{
                   "LogicalOperator": "And",
                   "FilterParams": [
                       { "Name": "Age", "Operator": "IsGreaterThan", "Value": 13, "DisplayName": "Возраст" },
                       { "Name": "Age", "Operator": "IsLessThan", "Value": 31, "DisplayName": "Возраст" }
                   ],
                   "JsonFilterManagers": [{
                       "LogicalOperator": "Or",
                       "FilterParams": [
                           { "Name": "Age", "Operator": "IsGreaterThan", "Value": 13, "DisplayName": "Возраст" },
                           { "Name": "Age", "Operator": "IsLessThan", "Value": 31, "DisplayName": "Возраст" }
                       ],
                       "JsonFilterManagers": [{
                           "LogicalOperator": "And",
                           "FilterParams": [
                               { "Name": "Age", "Operator": "IsGreaterThan", "Value": 13, "DisplayName": "Возраст" },
                               { "Name": "Age", "Operator": "IsLessThan", "Value": 31, "DisplayName": "Возраст" }
                           ],
                           "JsonFilterManagers": []
                       }]
                   }]
               }]
           },
           "Sorters": {
               "SortParams": [{ "Name": "Age", "Sort": "Descending", "DisplayName": "Возраст" }]
           },
           "Groups": {
               "GroupParams": [{ "FieldName": "Age", "DisplayName": "Возраст" }],
               "FieldNameForCount": "Weight"
           }
       };

    //var queryParams = ko.observable(
    //   {
    //       "EntityName": ko.observable("Person"),
    //       "Filters": ko.observable({
    //           "LogicalOperator": ko.observable("Or"),
    //           "FilterParams": ko.observableArray([
    //               { "Name": "Age", "Operator": "IsGreaterThan", "Value": 13, "DisplayName": "Возраст" },
    //               { "Name": "Age", "Operator": "IsLessThan", "Value": 31, "DisplayName": "Возраст" }
    //           ]),
    //           "JsonFilterManagers": ko.observable({
    //               "LogicalOperator": ko.observable("And"),
    //               "FilterParams": ko.observableArray([
    //                   { "Name": "Age", "Operator": "IsGreaterThan", "Value": 13, "DisplayName": "Возраст" },
    //                   { "Name": "Age", "Operator": "IsLessThan", "Value": 31, "DisplayName": "Возраст" }
    //               ]),
    //               "JsonFilterManagers": []
    //           })
    //       }),
    //       "Sorters": ko.observable({
    //           "SortParams": ko.observableArray([{ "Name": "Age", "Sort": "Descending", "DisplayName": "Возраст" }])
    //       }),
    //       "Groups": ko.observable({
    //           "GroupParams": ko.observableArray([{ "FieldName": "Age", "DisplayName": "Возраст" }]),
    //           "FieldNameForCount": ko.observable("Weight")
    //       })
    //   });

    demo.services.personService = {
        get: function (callback, jsonData) {
            if (callback != undefined)
                central.fw.ajaxService.getJson("loadpersons", jsonData, callback);
            else throw "callback is undefined";
        }
    }

    demo.vm.person = function () {
        var self = this,
            items = ko.observableArray([]),
            isbusy = ko.observable(false),
            queryBuilder = central.controls.QueryBuilder("home", "person", queryParams),
        callback = function (json) {
            isbusy(false);
            ko.mapping.fromJS(json, {}, items);
        },
        loaditems = function () {
            isbusy(true);
            var queryParams = ko.mapping.toJS(queryBuilder.queryParams);
            demo.services.personService.get(demo.vm.person.callback, { "queryParams": JSON.stringify(queryParams) });
        };
        return {
            viewname: queryBuilder.queryParams().EntityName,
            isbusy: isbusy,
            items: items,
            loadData: loaditems,
            callback: callback
        };
    }();
})(demo);

$(function () {
    //demo.vm.person.loadData();
    ko.applyBindings(demo.vm.person.queryBuilder.queryParams().Filters, $("#queryBuilder")[0]);
});